cmake_minimum_required(VERSION 3.16)
project(3dModelOptimizer VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 查找Qt5
find_package(Qt5 COMPONENTS Core Widgets OpenGL REQUIRED)

# 查找Assimp - 多种查找方式
set(ASSIMP_FOUND FALSE)

# 方法1: 使用find_package查找
find_package(assimp QUIET)
if(assimp_FOUND)
    set(ASSIMP_FOUND TRUE)
    message(STATUS "Found Assimp via find_package")
endif()

# 方法2: 使用pkg-config查找
if(NOT ASSIMP_FOUND)
    find_package(PkgConfig QUIET)
    if(PkgConfig_FOUND)
        pkg_check_modules(ASSIMP QUIET assimp)
        if(ASSIMP_FOUND)
            message(STATUS "Found Assimp via pkg-config")
        endif()
    endif()
endif()

# 方法3: 手动查找
if(NOT ASSIMP_FOUND)
    # 查找头文件
    find_path(ASSIMP_INCLUDE_DIR
        NAMES assimp/Importer.hpp
        PATHS ${ASSIMP_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/third_party/assimp
        PATH_SUFFIXES include include/assimp
    )
    
    # 查找库文件
    find_library(ASSIMP_LIBRARY
        NAMES assimp
        PATHS ${ASSIMP_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/third_party/assimp
        PATH_SUFFIXES lib lib64
    )
    
    if(ASSIMP_INCLUDE_DIR AND ASSIMP_LIBRARY)
        set(ASSIMP_FOUND TRUE)
        message(STATUS "Found Assimp manually: ${ASSIMP_INCLUDE_DIR}")
    endif()
endif()

# 如果仍然找不到，提供详细的错误信息
if(NOT ASSIMP_FOUND)
    message(FATAL_ERROR "
    ========================================
    Assimp库未找到！
    ========================================
    
    请按照以下步骤安装Assimp库：
    
    Windows用户:
    1. 使用vcpkg: vcpkg install assimp
    2. 或下载预编译版本: https://github.com/assimp/assimp/releases
    3. 或手动编译源码
    
    Linux用户:
    1. Ubuntu/Debian: sudo apt-get install libassimp-dev
    2. CentOS/RHEL: sudo yum install assimp-devel
    3. 或使用vcpkg: vcpkg install assimp
    
    macOS用户:
    1. 使用Homebrew: brew install assimp
    2. 或使用vcpkg: vcpkg install assimp
    
    安装后，请确保：
    1. 库文件在系统PATH中
    2. 头文件路径正确
    3. 重新运行CMake配置
    
    如果问题仍然存在，请手动指定Assimp路径：
    cmake .. -DASSIMP_DIR=/path/to/assimp
    ")
endif()

# 设置源文件
file(GLOB_RECURSE SOURCES
    "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/*.h"
    "${CMAKE_CURRENT_SOURCE_DIR}/*.ui"
)

# 创建可执行文件
add_executable(${PROJECT_NAME} ${SOURCES})

# 链接Qt库
target_link_libraries(${PROJECT_NAME} 
    Qt5::Core 
    Qt5::Widgets
    Qt5::OpenGL
)

# 链接Assimp库
if(assimp_FOUND)
    target_link_libraries(${PROJECT_NAME} assimp::assimp)
elseif(ASSIMP_FOUND)
    target_link_libraries(${PROJECT_NAME} ${ASSIMP_LIBRARIES})
    target_include_directories(${PROJECT_NAME} PRIVATE ${ASSIMP_INCLUDE_DIRS})
endif()

# 设置包含目录
target_include_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# 设置编译选项
if(MSVC)
    target_compile_options(${PROJECT_NAME} PRIVATE /W4)
else()
    target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra)
endif()

# 设置输出目录
set_target_properties(${PROJECT_NAME} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

# 安装规则
install(TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION bin
)

# 复制依赖库（Windows）
if(WIN32)
    # 复制Qt DLL
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:Qt5::Core>
        $<TARGET_FILE:Qt5::Widgets>
        $<TARGET_FILE_DIR:${PROJECT_NAME}>
    )
    
    # 复制Assimp DLL
    if(assimp_FOUND AND TARGET assimp::assimp)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            $<TARGET_FILE:assimp::assimp>
            $<TARGET_FILE_DIR:${PROJECT_NAME}>
        )
    elseif(ASSIMP_FOUND AND ASSIMP_LIBRARY)
        # 尝试复制手动找到的库文件
        get_filename_component(ASSIMP_LIBRARY_DIR ${ASSIMP_LIBRARY} DIRECTORY)
        file(GLOB ASSIMP_DLLS "${ASSIMP_LIBRARY_DIR}/*.dll")
        if(ASSIMP_DLLS)
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different
                ${ASSIMP_DLLS}
                $<TARGET_FILE_DIR:${PROJECT_NAME}>
            )
        endif()
    endif()
endif()

# 打印配置信息
message(STATUS "Project: ${PROJECT_NAME}")
message(STATUS "Version: ${PROJECT_VERSION}")
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Assimp Found: ${ASSIMP_FOUND}")
if(ASSIMP_FOUND)
    message(STATUS "Assimp Include Dir: ${ASSIMP_INCLUDE_DIRS}")
    message(STATUS "Assimp Libraries: ${ASSIMP_LIBRARIES}")
endif() 